<?php
require_once (__DIR__ . "/../../models/Products.php");
require_once (__DIR__ . "/../../models/Keywords.php");

if(isset($_POST['id']))
{
	$row = $_POST;
	$response = Products::Validate($row);
	if($response === true)
		$response = Products::Update($row);
	if($response === true)
		header("Location: $rootUrl/../W/Products/index");
}else {
	$row = Products::Get($_REQUEST['id']);
}
?>
				<?php if(isset($response)): ?>
					<dl class="dl-horizontal error">
						<?php foreach ($response as $key => $value): ?>
							<dt><?php echo $key?></dt>
							<dd><?php echo $value?></dd>
						<?php endforeach; ?>						
					</dl>
				<?php endif; ?>
				<form class="form-horizontal" action="" method="post">
					<input type="hidden" name="id" value="<?php echo $row['id']?>" />
					<div class="control-group">
						<label class="control-label" for="name">Name:</label>
						<div class="controls">
							<input type="text" name="name" id="name" value="<?php echo $row['name'];?>" class="<?php echo isset($response['name']) ? 'error' : '' ?>"
								/>
								<?php if(isset($response['name'])): ?>
									<span class="error"><?php echo $response['name']?></span>
								<?php endif; ?>


						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label" for="sku">SKU:</label>
						<div class="controls">
							<input type="text" name="sku" value="<?php echo $row['sku'];?>" class="<?php echo isset($response['sku']) ? 'error' : '' ?>"
								/>
								<?php if(isset($response['sku'])): ?>
									<span class="error"><?php echo $response['sku']?></span>
								<?php endif; ?>
						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="shortDescription">Short Description:</label>
						<div class="controls">
							<textarea name="shortDescription" cols="25" rows="5
class="<?php echo isset($response['shortDescription']) ? 'error' : '' ?>" ><?php echo $row['shortDescription']?></textarea>
								<?php if(isset($response['shortDescription'])): ?>
									<span class="error"><?php echo $response['shortDescription']?></span>
								<?php endif; ?>
						</div>
					</div>		
					<div class="control-group">
						<label class="control-label" for="longDescription">Long Description:</label>
						<div class="controls">
							<textarea name="longDescription" cols="25" rows="5"
class="<?php echo isset($response['longDescription']) ? 'error' : '' ?>" ><?php echo $row['longDescription']?></textarea>
								<?php if(isset($response['longDescription'])): ?>
									<span class="error"><?php echo $response['longDescription']?></span>
								<?php endif; ?>
						</div>
					</div>		
					<div class="control-group">
						<label class="control-label" for="thumb">Thumb:</label>
						<div class="controls">						
							<input type="text" name="thumb" value="<?php echo $row['thumb']?>"  />
						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="image" >Image:</label>
						<div class="controls">						
							<input type="text" name="image" value="<?php echo $row['image']?>"  />
						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="category">Category:</label>
						<div class="controls">
							
<?php $results = Keywords::GetChildren('categories');  
							echo '<select name="category">';
	  while($row2 = $results->fetch_assoc()): 
		if ($row2['id']==$row['category'])
	  			echo '<option value="'.$row2['id'].'" selected="selected">'.$row2['name'].'</option>';					
		else
	  			echo '<option value="'.$row2['id'].'" >'.$row2['name'].'</option>';					
	  endwhile;	
	   						echo '</select>';
						?> 
						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="numInStock">Num In Stock:</label>
						<div class="controls">
							<input type="number" name="numInStock" value="<?php echo $row['numInStock']?>" 
class="<?php echo isset($response['numInStock']) ? 'error' : '' ?>"
								/>
								<?php if(isset($response['numInStock'])): ?>
									<span class="error"><?php echo $response['numInStock']?></span>
								<?php endif; ?>

						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="price">Price:</label>
						<div class="controls">
							<input type="number" name="price" value="<?php echo $row['price']?>" 
class="<?php echo isset($response['price']) ? 'error' : '' ?>"
								/>
								<?php if(isset($response['price'])): ?>
									<span class="error"><?php echo $response['price']?></span>
								<?php endif; ?>

						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="location">Location:</label>
						<div class="controls">
							<input type="text" name="location" value="<?php echo $row['location']?>" 
class="<?php echo isset($response['location']) ? 'error' : '' ?>"
								/>
								<?php if(isset($response['location'])): ?>
									<span class="error"><?php echo $response['location']?></span>
								<?php endif; ?>

						</div>
					</div>
					<div class="control-group">
						<div class="controls">
							<input type="submit" value="Save" class="btn btn-primary" />
						</div>
					</div>
			
				</form>